package demo.activemq;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;
import java.util.Scanner;

public class Producer {
    /**
     * 一、点对点模式
     * @throws JMSException
     */
    public static void produce() throws JMSException {
        //1.创建连接工厂
        ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
        //2.获取连接
        Connection connection = factory.createConnection();
        //3.启动连接
        connection.start();
        /**
         *4.获取Session
         * 参数一：是否使用事务
         * 参数二：消息的确认模式
         */
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        //5.创建队列对象
        Queue queue = session.createQueue("myqueue");
        //6.创建消息生产者
        MessageProducer producer = session.createProducer(queue);
        //7.创建消息
        TextMessage message = null;
        Scanner scanner = new Scanner(System.in);
        while(true) {
            System.out.println("please input msg here(input 'exit' to exit):");
            String msg = scanner.nextLine();
            if("exit".equals(msg)) break;
            message = session.createTextMessage(msg);
            //8.发送消息
            producer.send(message);
        }
        //9.关闭资源
        producer.close();
        session.close();
        connection.close();
    }

    public static void main(String[] args) throws JMSException {
        produce();
    }
}
